home *** CD-ROM | disk | FTP | other *** search
Text File | 1999-07-24 | 14.6 KB | 576 lines | [TEXT/CWIE] |
- // UAdLibLink.cp
- // Copyright © 1993-1995 Nick Nallick. All rights reserved.
- //
- //
- // Changes:
- //
- // • Add MacApp 3.3 compatibility.
- // • Add MacApp 3.1 compatibility.
- //
-
- #ifndef __RESOURCES__
- #include <Resources.h>
- #endif
-
- #ifndef __MacAppCompatibility__
- #include "MacAppCompatibility.h"
- #endif
-
- #if qMacAppVersion != 30 && !defined(__LOWMEM__)
- #include "LowMem.h"
- #endif
-
- #pragma segment AdLibLink
-
-
- ///////////////////////////////// TAdLibLink /////////////////////////////////
- //
- // A behavior to receive AppleEvents from Ad Lib to update the application's
- // 'View' resources. Add this object to your build and derive your application
- // object from TBehaviorApp. Finally, include the AdLibLink.r file in your build.
- // This will permit your application to be used with the 'Link Application'
- // function in Ad Lib.
- //
-
- class TAdLibLink : public TBehavior
- {
- #if qMacAppVersion > 30
- MA_DECLARE_CLASS;
- #endif
-
- public:
- #if qMacAppVersion > 33
- TAdLibLink(IDType itsIdentifier = kNoIdentifier_AC);
- virtual ~TAdLibLink();
- #else
- VIRTUAL void IAdLibLink(IDType itsIdentifier);
- #endif
-
- #if qMacAppVersion == 30 || qMacAppVersion == 31
- VIRTUAL void DoAppleCommand(CommandNumber cmd, const AppleEvent& message, const AppleEvent& reply);
- #else
- VIRTUAL BOOLEAN DoScriptCommand(CommandNumber cmd, TAppleEvent* message, TAppleEvent* reply);
- #endif
-
- private:
- void Add(const AppleEvent& message, AppleEvent& reply);
- void Change(const AppleEvent& message, AppleEvent& reply);
- void Remove(const AppleEvent& message, AppleEvent& reply);
- void Set(const AppleEvent& message, AppleEvent& reply);
- };
-
- #if qMacAppVersion > 30
- MA_DEFINE_CLASS_M1(TAdLibLink, TBehavior);
- #endif
-
-
- #pragma segment MABehaviorRes
-
- const short mPingMsg = 990;
- const short mAddMsg = 991;
- const short mChangeMsg = 992;
- const short mRemoveMsg = 993;
- const short mSetMsg = 994;
- const short mUpdateMsg = 995;
-
- #if qMacAppVersion > 33
-
- // Constructor
- // itsIdentifier: the behavior's identifier
- //
- TAdLibLink::TAdLibLink(IDType itsIdentifier) :
- TBehavior(itsIdentifier)
- {
- }
-
- // Added destructor -- MB-9805-29-1941
- TAdLibLink::~TAdLibLink()
- {
- }
-
- #endif
-
- #if qMacAppVersion == 30 || qMacAppVersion == 31
-
- // Overrides TBehavior
- // Handle Ad Lib's resource saving AppleEvents.
- //
- MACAPP_METHOD
- void TAdLibLink::DoAppleCommand(CommandNumber cmd, const AppleEvent& message, const AppleEvent& reply)
- {
- switch (cmd)
- {
- case mPingMsg: break;
- case mAddMsg: Add(message, reply);
- break;
- case mChangeMsg: Change(message, reply);
- break;
- case mRemoveMsg: Remove(message, reply);
- break;
- case mSetMsg: Set(message, reply);
- break;
- case mUpdateMsg: UpdateResFile(gApplicationRefNum);
- THROWIFRESERROR();
- break;
- default: TBehavior::DoAppleCommand(cmd, message, reply);
- break;
- }
- }
-
- #else
-
- // Overrides TBehavior
- // Handle Ad Lib's resource saving AppleEvents.
- //
- // bpa 6/20/97 - changed fMessage to GetAEDesc
-
- inline AEDesc GetAEDesc(TAppleEvent* event)
- #if qMacAppVersion > 33
- { return *event; }
- #else
- { return event->fMessage; }
- #endif
-
- MACAPP_METHOD
- BOOLEAN TAdLibLink::DoScriptCommand(CommandNumber cmd, TAppleEvent* message, TAppleEvent* reply)
- {
- switch (cmd)
- {
- case mPingMsg: break;
- case mAddMsg:
- { // Add temp variables to get rid of compiler warning -- MB-9907-17-1010
- AEDesc msgDesc = GetAEDesc(message);
- AEDesc replyDesc = GetAEDesc(reply);
- Add(msgDesc, replyDesc);
- }
- break;
- case mChangeMsg:
- { // Add temp variables to get rid of compiler warning -- MB-9907-17-1010
- AEDesc msgDesc = GetAEDesc(message);
- AEDesc replyDesc = GetAEDesc(reply);
- Change(msgDesc, replyDesc);
- }
- break;
- case mRemoveMsg:
- { // Add temp variables to get rid of compiler warning -- MB-9907-17-1010
- AEDesc msgDesc = GetAEDesc(message);
- AEDesc replyDesc = GetAEDesc(reply);
- Remove(msgDesc, replyDesc);
- }
- break;
- case mSetMsg:
- { // Add temp variables to get rid of compiler warning -- MB-9907-17-1010
- AEDesc msgDesc = GetAEDesc(message);
- AEDesc replyDesc = GetAEDesc(reply);
- Set(msgDesc, replyDesc);
- }
- break;
- case mUpdateMsg: UpdateResFile(gApplicationRefNum);
- THROWIFRESERROR();
- break;
- default: return TBehavior::DoScriptCommand(cmd, message, reply);
- }
-
- return TRUE;
- }
-
- #endif
-
-
- #pragma segment MAOpen
-
- // Initialize the behavior.
- //
- // itsIdentifier: the behavior's identifier
- //
- #if qMacAppVersion <= 33
- MACAPP_METHOD
- void TAdLibLink::IAdLibLink(IDType itsIdentifier)
- {
- IBehavior(itsIdentifier);
- }
- #endif
-
-
- #pragma segment AdLibLink
-
- #if qMacAppVersion == 30
-
- // Handle the add resource AppleEvent.
- //
- // message: the AppleEvent
- // reply: the reply AppleEvent
- //
- void TAdLibLink::Add(const AppleEvent& message, const AppleEvent& reply)
- {
- short id, attributes;
- ResType type;
- DescType actualMsgType;
- Size size;
-
- THROWIFOSERR(AEGetParamPtr(message, 'RTYP', 'RSRC', actualMsgType, (Ptr) &type, sizeof(type), size));
- THROWIFOSERR(AEGetParamPtr(message, 'RID ', typeShortInteger, actualMsgType, (Ptr) &id, sizeof(id), size));
- OSErr error = AEGetParamPtr(message, 'ATTR', typeShortInteger, actualMsgType, (Ptr) &attributes, sizeof(id), size);
- Boolean setAttributes = (error == noErr);
- if (error == errAEDescNotFound)
- error = noErr;
- THROWIFOSERR(error);
- THROWIFOSERR(AESizeOfParam(message, 'DATA', actualMsgType, size));
-
- if (size > 0)
- {
- MAVolatileInit(Handle, data, NULL);
- FailInfo fi;
- TRY(fi)
- {
- data = NewPermHandle(size);
- HLock(data);
- THROWIFOSERR(AEGetParamPtr(message, 'DATA', 'data', actualMsgType, *data, size, size));
- HUnlock(data);
- short curRsrcFile = USERESFILE(gApplicationRefNum);
- AddResource(data, type, id, gEmptyString);
- error = ResError();
- USERESFILE(curRsrcFile);
- THROWIFOSERR(error);
- if (setAttributes)
- {
- SetResAttrs(data, attributes);
- THROWIFRESERROR();
- ChangedResource(data);
- THROWIFRESERROR();
- }
- fi.Success();
- }
- else
- {
- DISPOSEIFHANDLE(data);
- error = fi.error;
- }
- }
- THROWIFOSERR(AEPutParamPtr(reply, keyErrorNumber, typeShortInteger, (Ptr) &error, sizeof(error)));
- }
-
-
- // Handle the changed resource AppleEvent.
- //
- // message: the AppleEvent
- // reply: the reply AppleEvent
- //
- void TAdLibLink::Change(const AppleEvent& message, const AppleEvent& reply)
- {
- short id;
- ResType type;
- DescType actualMsgType;
- Size size;
- OSErr error;
-
- THROWIFOSERR(AEGetParamPtr(message, 'RTYP', 'RSRC', actualMsgType, (Ptr) &type, sizeof(type), size));
- THROWIFOSERR(AEGetParamPtr(message, 'RID ', typeShortInteger, actualMsgType, (Ptr) &id, sizeof(id), size));
- THROWIFOSERR(AESizeOfParam(message, 'DATA', actualMsgType, size));
-
- short curRsrcFile = USERESFILE(gApplicationRefNum);
- Boolean oldPerm = PermAllocation(TRUE);
- Handle rsrc = Get1Resource(type, id);
- error = ResError();
- PermAllocation(oldPerm);
- USERESFILE(curRsrcFile);
- if (error == noErr)
- {
- FailInfo fi;
- TRY(fi)
- {
- SetHandleSize(rsrc, size);
- FailMemError();
- HLock(rsrc);
- THROWIFOSERR(AEGetParamPtr(message, 'DATA', 'data', actualMsgType, *rsrc, size, size));
- HUnlock(rsrc);
- ChangedResource(rsrc);
- THROWIFRESERROR();
- fi.Success();
- }
- else
- error = fi.error;
- }
- THROWIFOSERR(AEPutParamPtr(reply, keyErrorNumber, typeShortInteger, (Ptr) &error, sizeof(error)));
- }
-
-
- // Handle the remove resource AppleEvent.
- //
- // message: the AppleEvent
- // reply: the reply AppleEvent
- //
- void TAdLibLink::Remove(const AppleEvent& message, const AppleEvent& reply)
- {
- short id;
- ResType type;
- DescType actualMsgType;
- Size size;
- OSErr error;
-
- THROWIFOSERR(AEGetParamPtr(message, 'RTYP', 'RSRC', actualMsgType, (Ptr) &type, sizeof(type), size));
- THROWIFOSERR(AEGetParamPtr(message, 'RID ', typeShortInteger, actualMsgType, (Ptr) &id, sizeof(id), size));
-
- short curRsrcFile = USERESFILE(gApplicationRefNum);
- Boolean curResLoad = LMGetResLoad();
- SetResLoad(FALSE);
- Handle rsrc = Get1Resource(type, id);
- error = ResError();
- SetResLoad(curResLoad);
- if (error == noErr)
- {
- RemoveResource(rsrc);
- error = ResError();
- }
- USERESFILE(curRsrcFile);
- THROWIFOSERR(AEPutParamPtr(reply, keyErrorNumber, typeShortInteger, (Ptr) &error, sizeof(error)));
- }
-
-
- // Handle the set resource info AppleEvent.
- //
- // message: the AppleEvent
- // reply: the reply AppleEvent
- //
- void TAdLibLink::Set(const AppleEvent& message, const AppleEvent& reply)
- {
- short oldID, newID;
- ResType type;
- CStr255 name;
- DescType actualMsgType;
- Size size;
- OSErr error;
-
- THROWIFOSERR(AEGetParamPtr(message, 'RTYP', 'RSRC', actualMsgType, (Ptr) &type, sizeof(type), size));
- THROWIFOSERR(AEGetParamPtr(message, 'RID ', typeShortInteger, actualMsgType, (Ptr) &oldID, sizeof(oldID), size));
- THROWIFOSERR(AEGetParamPtr(message, 'ID ', typeShortInteger, actualMsgType, (Ptr) &newID, sizeof(newID), size));
- THROWIFOSERR(AEGetParamPtr(message, 'NAME', typeChar, actualMsgType, (Ptr) &name[1], sizeof(name) - 1, size));
- name.Length() = (char) size;
-
- short curRsrcFile = USERESFILE(gApplicationRefNum);
- Boolean curResLoad = LMGetResLoad();
- SetResLoad(FALSE);
- Handle rsrc = Get1Resource(type, oldID);
- error = ResError();
- SetResLoad(curResLoad);
- if (error == noErr)
- {
- SetResInfo(rsrc, newID, name);
- error = ResError();
- }
- USERESFILE(curRsrcFile);
- THROWIFOSERR(AEPutParamPtr(reply, keyErrorNumber, typeShortInteger, (Ptr) &error, sizeof(error)));
- }
-
- #else
-
- // Handle the add resource AppleEvent.
- //
- // message: the AppleEvent
- // reply: the reply AppleEvent
- //
- void TAdLibLink::Add(const AppleEvent& message, AppleEvent& reply)
- {
- short id, attributes;
- ResType type;
- DescType actualMsgType;
- Size size;
-
- THROWIFOSERR(AEGetParamPtr(&message, 'RTYP', 'RSRC', &actualMsgType, &type, sizeof(type), &size));
- THROWIFOSERR(AEGetParamPtr(&message, 'RID ', typeShortInteger, &actualMsgType, &id, sizeof(id), &size));
- OSErr error = AEGetParamPtr(&message, 'ATTR', typeShortInteger, &actualMsgType, &attributes, sizeof(id), &size);
- Boolean setAttributes = (error == noErr);
- if (error == errAEDescNotFound)
- error = noErr;
- THROWIFOSERR(error);
- THROWIFOSERR(AESizeOfParam(&message, 'DATA', &actualMsgType, &size));
-
- if (size > 0)
- {
- MAVolatileInit(Handle, data, NULL);
- #if qMacAppVersion <= 33
- FailInfo fi;
- TRY(fi)
- #else
- try
- #endif
- {
- data = NewPermHandle(size);
- HLock(data);
- THROWIFOSERR(AEGetParamPtr(&message, 'DATA', 'data', &actualMsgType, *data, size, &size));
- HUnlock(data);
- short curRsrcFile = USERESFILE(gApplicationRefNum);
- AddResource(data, type, id, gEmptyString);
- error = ResError();
- USERESFILE(curRsrcFile);
- THROWIFOSERR(error);
- if (setAttributes)
- {
- SetResAttrs(data, attributes);
- THROWIFRESERROR();
- ChangedResource(data);
- THROWIFRESERROR();
- }
- #if qMacAppVersion <= 33
- fi.Success();
- #endif
- }
- #if qMacAppVersion <= 33
- else
- #else
- catch (CException_AC& theException)
- #endif
- {
- DISPOSEIFHANDLE(data);
- #if qMacAppVersion <= 33
- error = fi.error;
- #else
- error = theException.GetError();
- #endif
- }
- }
- THROWIFOSERR(AEPutParamPtr(&reply, keyErrorNumber, typeShortInteger, &error, sizeof(error)));
- }
-
-
- // Handle the changed resource AppleEvent.
- //
- // message: the AppleEvent
- // reply: the reply AppleEvent
- //
- void TAdLibLink::Change(const AppleEvent& message, AppleEvent& reply)
- {
- short id;
- ResType type;
- DescType actualMsgType;
- Size size;
- OSErr error;
-
- THROWIFOSERR(AEGetParamPtr(&message, 'RTYP', 'RSRC', &actualMsgType, &type, sizeof(type), &size));
- THROWIFOSERR(AEGetParamPtr(&message, 'RID ', typeShortInteger, &actualMsgType, &id, sizeof(id), &size));
- THROWIFOSERR(AESizeOfParam(&message, 'DATA', &actualMsgType, &size));
-
- short curRsrcFile = USERESFILE(gApplicationRefNum);
- Boolean oldPerm = PermAllocation(TRUE);
- Handle rsrc = Get1Resource(type, id);
- error = ResError();
- PermAllocation(oldPerm);
- USERESFILE(curRsrcFile);
- if (error == noErr)
- {
- #if qMacAppVersion <= 33
- FailInfo fi;
- TRY(fi)
- #else
- try
- #endif
- {
- SetHandleSize(rsrc, size);
- THROWIFMEMERROR();
- HLock(rsrc);
- THROWIFOSERR(AEGetParamPtr(&message, 'DATA', 'data', &actualMsgType, *rsrc, size, &size));
- HUnlock(rsrc);
- ChangedResource(rsrc);
- THROWIFRESERROR();
- #if qMacAppVersion <= 33
- fi.Success();
- #endif
- }
- #if qMacAppVersion <= 33
- else
- error = fi.error;
- #else
- catch (CException_AC& theException)
- {
- error = theException.GetError();
- }
- #endif
- }
- THROWIFOSERR(AEPutParamPtr(&reply, keyErrorNumber, typeShortInteger, &error, sizeof(error)));
- }
-
-
- // Handle the remove resource AppleEvent.
- //
- // message: the AppleEvent
- // reply: the reply AppleEvent
- //
- void TAdLibLink::Remove(const AppleEvent& message, AppleEvent& reply)
- {
- short id;
- ResType type;
- DescType actualMsgType;
- Size size;
- OSErr error;
-
- THROWIFOSERR(AEGetParamPtr(&message, 'RTYP', 'RSRC', &actualMsgType, &type, sizeof(type), &size));
- THROWIFOSERR(AEGetParamPtr(&message, 'RID ', typeShortInteger, &actualMsgType, &id, sizeof(id), &size));
-
- short curRsrcFile = USERESFILE(gApplicationRefNum);
- Boolean curResLoad = LMGetResLoad();
- SetResLoad(FALSE);
- Handle rsrc = Get1Resource(type, id);
- error = ResError();
- SetResLoad(curResLoad);
- if (error == noErr)
- {
- RemoveResource(rsrc);
- error = ResError();
- }
- USERESFILE(curRsrcFile);
- THROWIFOSERR(AEPutParamPtr(&reply, keyErrorNumber, typeShortInteger, (Ptr) &error, sizeof(error)));
- }
-
-
- // Handle the set resource info AppleEvent.
- //
- // message: the AppleEvent
- // reply: the reply AppleEvent
- //
- void TAdLibLink::Set(const AppleEvent& message, AppleEvent& reply)
- {
- short oldID, newID;
- ResType type;
- CSTR255 name;
- DescType actualMsgType;
- Size size;
- OSErr error;
-
- THROWIFOSERR(AEGetParamPtr(&message, 'RTYP', 'RSRC', &actualMsgType, &type, sizeof(type), &size));
- THROWIFOSERR(AEGetParamPtr(&message, 'RID ', typeShortInteger, &actualMsgType, &oldID, sizeof(oldID), &size));
- THROWIFOSERR(AEGetParamPtr(&message, 'ID ', typeShortInteger, &actualMsgType, &newID, sizeof(newID), &size));
- THROWIFOSERR(AEGetParamPtr(&message, 'NAME', typeChar, &actualMsgType, &name[1], sizeof(name) - 1, &size));
- name.Length() = (char) size;
-
- short curRsrcFile = USERESFILE(gApplicationRefNum);
- Boolean curResLoad = LMGetResLoad();
- SetResLoad(FALSE);
- Handle rsrc = Get1Resource(type, oldID);
- error = ResError();
- SetResLoad(curResLoad);
- if (error == noErr)
- {
- SetResInfo(rsrc, newID, name);
- error = ResError();
- }
- USERESFILE(curRsrcFile);
- THROWIFOSERR(AEPutParamPtr(&reply, keyErrorNumber, typeShortInteger, &error, sizeof(error)));
- }
-
- #endif
-
-
- //////////////////////////////////////////////////////////////////////////////
-
- #pragma segment AInit
-
- void RegisterTAdLibLink(); // function prototype
-
- void RegisterTAdLibLink()
- {
- #if qMacAppVersion == 30
- new TAdLibLink;
- #else
- MA_REGISTER_CLASS(TAdLibLink);
- #endif
- }